<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a
        @click="hasAccount = true"
        :class="{ active: hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId" />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref, getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { userQQLogin } from '@/api/user'
import QC from 'qc'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup() {
    const hasAccount = ref(true)
    const nickname = ref(null)
    const avatar = ref(null)
    const store = useStore()
    const router = useRouter()
    const unionId = ref(null)
    const { proxy } = getCurrentInstance()
    // 默认已经绑定过
    const isBind = ref(true)
    // 首先：默认认为已经注册且已经绑定
    // 通过qq的apiopenID就是后台需要的unionId然后去进行登录
    // 如果成功：登录成功
    // 如果失败：改qq未和小兔仙进行绑定（有账号未绑定qq，没有账号未绑定qq）
    // 如果QC.login.check为真
    if (QC.Login.check()) {
      QC.Login.getMe((openId) => {
        unionId.value = openId
        // 请求小兔先后台，做qq登录
        userQQLogin(openId)
          .then((data) => {
            // 登录成功：data.result用户信息
            // 存储到用户信息
            // 跳转到来源页或首页
            const { id, account, avatar, mobile, nickname, token } = data.result
            // 存储用户数据
            store.commit('user/setUser', {
              id,
              account,
              avatar,
              mobile,
              nickname,
              token
            })
            store.dispatch('cart/metgeCart').then(() => {
              router.pash(store.state.user.setRedirectUrl)
              proxy.$message('success', 'QQ登录成功')
            })
          })
          .catch((e) => {
            // 登录失败，没有绑定过
            isBind.value = false
          })
      })
    }
    return { hasAccount, nickname, avatar, isBind, unionId }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}
</style>
